/*
 * Copyright (c) 2012-2012 XXXX XXXXXX, Inc.
 * XXXX Road, XXXX, XXXX State, XXXX Post code, XXXX Country.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of XXXX
 * XXXXXX, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with XXXX.
 * 
 * History
 * Date        Ver Author        Change Description
 * ----------- --- ------------- ----------------------------------------
 * 08 Mar 2013 001 Andrew        verify customer name in email after choosing sro type to 'Service Under Contract' and check RCA
 * 08 Jun 2013 002 Karl          Update xpath of hotmail. 
 */
package test.com.sms.selenium.singlepoint.ticket.incident.edit;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import test.com.sms.selenium.framework.SeleniumTestAbstract;
import test.com.sms.selenium.singlepoint.util.SpHotmailInboxPageUtil;
import test.com.sms.selenium.singlepoint.util.SpHotmailLoginPageUtil;
import test.com.sms.selenium.singlepoint.util.SpIncidentEditPageUtil;
import test.com.sms.selenium.singlepoint.util.SpSearchPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpTicketingChooseRcaAndVerifyEmail extends SeleniumTestAbstract {
	@Test
	public void chooseRcaAndVerifyEmailContainsCustomer(){
		String incidentNumber = "337113";
		String sroTypeToContract = "Services under Contract";
		String sroTypeToDisposition = "Asset Disposition"; 
		try {
			// login and enter incident edit page
			SpSearchPageUtil.enter();
			SpIncidentEditPageUtil.enterIncidentID(incidentNumber);
			SpIncidentEditPageUtil.waitForEditPageToLoad();
			
			// change sro type, check RCA and save it
			SeleniumUtil.selectByVisibleText(new Select(
				SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_SELECT_SRO_TYPE)), sroTypeToContract);
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_CHECKBOX_RCA));
			SeleniumUtil.waitOnElements(new By[]{SpIncidentEditPageUtil.IE_BUTTON_MAIN_SAVE});
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_BUTTON_MAIN_SAVE));

			// the email should be sent. restore the sro type to 'Asset Disposition', RCA to uncheck and save it
			SeleniumUtil.selectByVisibleText(new Select(
				SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_SELECT_SRO_TYPE)), sroTypeToDisposition);
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_CHECKBOX_RCA));
			SeleniumUtil.waitOnElements(new By[]{SpIncidentEditPageUtil.IE_BUTTON_MAIN_SAVE});
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpIncidentEditPageUtil.IE_BUTTON_MAIN_SAVE));
			
			// enter hotmail inbox page
			SpHotmailLoginPageUtil.loginHotmail();
			WebElement mailLink = SpHotmailInboxPageUtil.findMailWithTextInSubject("Incident Updated");
			if(mailLink != null){
				SpHotmailInboxPageUtil.assertMailContainsText(mailLink, incidentNumber);
			} else {
				SeleniumUtil.fail("Can't find the email which contains incident number [" + incidentNumber + "]. ");
			}
		} catch (Exception e) {
			SeleniumUtil.getScreenShot(e);
		}
	}
}
